﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWordAddIn
{
    class Time
    {
        /// <summary>
                /// 时间转时间戳（精确到秒）
                /// </summary>
                /// <param name="date"></param>
                /// <returns></returns>
        public static long GetTimeStamp(DateTime date)
        {
            var ticks = date.ToUniversalTime().Ticks - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
            var timeSpan = ticks / TimeSpan.TicksPerSecond;
            return timeSpan;
        }
        /// <summary>
        /// 时间戳转时间
        /// </summary>
        /// <param name="timespan"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(long timespan)
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            var dt = timespan.ToString().Length == 13 ? startTime.AddMilliseconds(timespan) : startTime.AddSeconds(timespan);
            return dt;
        }
        /// <summary>
        /// 秒
        /// </summary>
        /// <returns></returns>
        public static long Timestamps()
        {
            return (DateTime.UtcNow.Ticks - 621355968000000000L) / 10000000L;
        }
        /// <summary>
        /// 毫秒
        /// </summary>
        /// <returns></returns>
        public static long Timestampms()
        {
            return (DateTime.UtcNow.Ticks - 621355968000000000L) / 10000L;
        }

    }
}
